
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEditor;
public class ParseGameObject : ParseObjectHandle<GameObject>
{

    public override void Prase(ExportSetting ExportSetting, GameObject obj, JArray jArray)
    {
        JToken nodeToken = ExportSetting.CocosObjectConfig["cc.Node"].DeepClone();
        AssetHelper.CreateGameObjectToken(nodeToken, obj);

        int parentId = 0;
        bool isPrefab = PrefabUtility.IsAnyPrefabInstanceRoot(obj);
        if (isPrefab)
        {
            var prefabroot = PrefabUtility.GetCorrespondingObjectFromSource(obj);

            var prefabNodeToken = ExportSetting.CocosObjectConfig["cc.PrefabNode"].DeepClone();
            prefabNodeToken["_id"] = IdHelper.GetCompressGuid();
            jArray.Add(prefabNodeToken);
            prefabNodeToken = jArray[jArray.Count - 1];
            prefabNodeToken["_prefab"] = AssetHelper.CreateIdJObject(jArray.Count);
            int ccprefabinfoIndex = jArray.Count;
            AssetHelper.AddPropertyGameObejct(ExportSetting, prefabroot, obj, jArray, nodeToken);
            var ccprefabinfo = jArray[ccprefabinfoIndex].DeepClone();
            var ccprefabinstance = jArray[ccprefabinfoIndex + 1].DeepClone();

            jArray = new JArray();
            var ccprefab = ExportSetting.CocosObjectConfig["cc.Prefab"].DeepClone();
            jArray.Add(ccprefab);
            ccprefab["data"] = AssetHelper.CreateIdJObject(1);
            jArray.Add(nodeToken);
            nodeToken = jArray[jArray.Count - 1];
            parentId = jArray.Count - 1;
            nodeToken["_parent"] = null;
            nodeToken["_prefab"] = AssetHelper.CreateIdJObject(jArray.Count);
            jArray.Add(ccprefabinfo);
            ccprefabinfo = jArray[jArray.Count - 1];
            ccprefabinfo["root"] = AssetHelper.CreateIdJObject(1);
            ccprefabinfo["asset"] = AssetHelper.CreateIdJObject(0);
            if (obj.transform.childCount > 0)
            {
                ccprefabinfo["instance"] = AssetHelper.CreateIdJObject(jArray.Count);
                jArray.Add(ccprefabinstance);
                ccprefabinstance = jArray[jArray.Count - 1];
                ccprefabinstance["prefabRootNode"] = AssetHelper.CreateIdJObject(1);
            }
            else
            {
                (ccprefabinfo as JObject).Remove("instance");
            }


        }
        else
        {
            jArray.Add(nodeToken);
            nodeToken = jArray[jArray.Count - 1];
            parentId = jArray.Count - 1;
        }



        JArray components = nodeToken["_components"] as JArray;
        components.Clear();
        foreach (var component in obj.GetComponents<Component>())
        {
            if (component == null)
            {
                continue;
            }
            if (ExportSetting.IgnoreNoEnable)
            {
                var property = component.GetType().GetProperty("enabled");
                if (property != null)
                {
                    var enable = (bool)property.GetValue(component);
                    if (!enable)
                    {
                        continue;
                    }
                }
            }

            int componentId = jArray.Count;
            ExportToDispatcher.Instance.ParseObjectToCocos(component, ExportSetting, jArray);
            int afterAddCount = jArray.Count;
            if (componentId == afterAddCount)
            {
                continue;
            }
            if (jArray.Count != componentId)
            {
                components.Add(AssetHelper.CreateIdJObject(componentId));
                JToken componentToken = jArray[componentId];
                componentToken["_id"] = IdHelper.GetCompressGuid();
                componentToken["node"] = AssetHelper.CreateIdJObject(parentId);
                //componentToken["_enabled"]=component.
            }
        }

        JArray nodeChildren = nodeToken["_children"] as JArray;
        nodeChildren.Clear();
        foreach (Transform item in obj.transform)
        {
            if (ExportSetting.IgnoreNoActive && !item.gameObject.activeSelf)
            {
                continue;
            }
            int itemId = jArray.Count;
            ExportToDispatcher.Instance.ParseObjectToCocos(item.gameObject, ExportSetting, jArray);

            nodeChildren.Add(AssetHelper.CreateIdJObject(itemId));
            JToken childToken = jArray[itemId];
            childToken["_parent"] = AssetHelper.CreateIdJObject(parentId);
        }
        if (isPrefab)
        {
            var prefabroot = PrefabUtility.GetCorrespondingObjectFromSource(obj);
            AssetHelper.CreatePrefab(ExportSetting, prefabroot, jArray);
        }
        //nodeToken[""]
    }
    private Dictionary<UnityEngine.Object, string> fieldMap = new Dictionary<UnityEngine.Object, string>();
}